// import join from '@feidao/web/atom/nodejs/db/join-inner';

// import count from '@feidao/web/atom/nodejs/db/count';
import query from '@dfeidao/atom-nodejs/db/query';
import format from '@dfeidao/atom-nodejs/dt/format';

interface Message {
	productid: string;
	usercode: string;
}
interface Star {
	role_no: string;
}
interface Data {
	role_no: string;
	role_name: string;
	role_type_id: string;
	icon: string;
	create_time: string;
}
export default async function atom(msg: Message, action_id: string, session_id: string, spaceid: string) {
	const systemid = '12';
	const { exec, prepare } = query(action_id, session_id, systemid);
	prepare('pub_user_connect', ['_id', 'usercode', 'role_no', 'pk_val', 'productid', 'search_field_val', 'create_time'], { productid: msg.productid, usercode: msg.usercode }, 200, 1, [], []);
	const [data] = await exec<Data>();
	data.forEach((item) => {
		item.create_time = item.create_time ? format(Number(item.create_time), 'YYYY-MM-DD') : '';
	});
	const r_nos = (data).map((s: Star) => {
		const r_no = s.role_no;
		return r_no;
	});
	const q = { role_no: { $in: r_nos }, productid: msg.productid };

	prepare('pub_role', ['_id', 'role_name', 'role_no', 'role_type_id', 'productid', 'icon', 'create_time'], q, 200, 1, [], []);
	const [data2] = await exec<Data>();
	// debugger
	// const d = join<any, any, any>(data, data2, 'role_no', 'role_no');
	// const detail = new Array();

	data.map((v) => {
		const role_no = v.role_no;
		const c_data = data2.filter((a) => {
			return a.role_no === role_no;
		});
		if (c_data.length > 0) {
			v.role_name = c_data[0].role_name;
			v.role_type_id = c_data[0].role_type_id;
			v.icon = c_data[0].icon;
		} else {
			v.role_name = '';
			v.role_type_id = '';
			v.icon = '';
		}
		return v;
	});


	// data.forEach((i) => {
	// 	data2.forEach((j) => {
	// 		if (i['role_no'] == j['role_no']) {
	// 			const obj = Object.assign(i, j);
	// 			detail.push(obj);
	// 		}
	// 	})
	// })
	// console.log(data, data2, '111111111111111111111111111111111')
	// const c = count(action_id, session_id, spaceid, systemid);
	// c.prepare('pub_user', msg.filter);
	// const [t] = await c.exec();

	return { dataArray: data };
}
